package com.zkw.group.controller;

import com.alibaba.fastjson.JSON;
import com.zkw.group.entity.CoursePage;
import com.zkw.group.entity.QueryInfo;
import com.zkw.group.service.CoursePageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@Api(description = "管理课程章节信息")
@CrossOrigin
@RestController
@RequestMapping("/CoursePages")
public class CoursePageController {

    @Autowired
    private CoursePageService coursePageService;

    @ApiOperation(value = "分页查询课程所有章节信息")
    @GetMapping("/AllCoursePage")
    public String getAllCoursePageList(QueryInfo queryInfo) {
        int numbers = coursePageService.getCoursePageCounts(queryInfo.getQuery(),queryInfo.getRole());
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<CoursePage> coursePages = coursePageService.getAllCoursePageList(queryInfo.getQuery(),queryInfo.getRole(),pageStart,queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",coursePages);
        return JSON.toJSONString(res);
    }

    @ApiOperation(value = "添加课程章节信息")
    @PostMapping("/CoursePage")
    public String addCoursePage(@RequestBody CoursePage coursePage) {
        int result =  coursePageService.addCoursePage(coursePage);
        if (result >= 1) {
            return "success";
        } else {
            return "error";
        }
    }

    @ApiOperation(value = "根据课程章节号删除课程章节信息")
    @DeleteMapping("/CoursePage")
    public String deleteCoursePage(String ord,String courseid) {
        int result = coursePageService.deleteCoursePage(ord,courseid);
        if (result >= 1) {
            return "success";
        } else {
            return "error";
        }
    }

    @ApiOperation(value = "根据课程章节号查询课程章节信息")
    @GetMapping("/CoursePageMessage")
    public String getUpdateCoursePage(String ord,String courseid) {
        CoursePage updateCoursePage = coursePageService.getUpdateCoursePage(ord,courseid);
        String users_json = JSON.toJSONString(updateCoursePage);
        return users_json;
    }

    @ApiOperation(value = "更新课程章节信息")
    @PutMapping("/CoursePage")
    public String updateCoursePage(@RequestBody CoursePage coursePage) {
        int result = coursePageService.updateCoursePage(coursePage);
        if (result >= 1) {
            return "success";
        } else {
            return "error";
        }
    }
}
